<template>
  <div class="head-wrapper" style="padding-bottom: 20px; text-align: right">
    <el-button @click="saveProduct" type="primary">保存</el-button>
  </div>
  <el-form :model="formInline" class="demo-form-inline" label-width="60px">
    <el-card shadow="always">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="分类:">
            <el-select
              v-model="formInline.cid"
              placeholder="请选择分类"
              size="large"
            >
              <el-option
                v-for="item in categories"
                :key="item.cid"
                :label="item.name"
                :value="item.cid"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称:">
            <el-input
              placeholder="请输入名称"
              v-model="formInline.name"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="实物图:">
            <UploadImage v-model="formInline.image" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="规格:">
            <ProductSpec v-model="formInline.specs" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-card>
  </el-form>
</template>
<script setup>
import { ref, onMounted, computed } from "vue";
import ProductSpec from "@/components/product-spec.vue";
import UploadImage from "@/components/upload.vue";
import { updateProduct, pCategoryList, pDetail } from "@/network/product";
import { useRouter } from "vue-router";
const router = useRouter();

const categories = ref([]);

onMounted(() => {
  pCategoryList({}).then((res) => {
    if (res.status === 1) {
      categories.value = res.data;
    }
  });
  pDetail({ id: router.currentRoute.value.query.id }).then((res) => {
    if (res.status === 1) {
      formInline.value = res.data;
    }
  });
});

const formInline = ref({
  att_name: "",
  image: [],
  name: "",
  specs: [
    {
      attribute: "微辣",
      unit: "盘",
      unitprice: 20.0,
    },
  ],
  unit: "",
  unitprice: "",
});

const saveProduct = () => {
  const formData = formInline.value;
  formData.unit = formData.specs[0].unit;
  formData.unitprice = formData.specs[0].unitprice;
  console.log(formData);
  updateProduct(formData).then((response) => {
    if (response.status === 1) {
      router.push({ path: "/product" });
    }
  });
};
</script>

<style scoped>
</style>